/*------------------------------

	Space Is The Place v 0.1
		by Sestze
		
	cl_economy.lua

------------------------------*/

local sin,cos,rad = math.sin,math.cos,math.rad;

function GeneratePoly(x, y, radius, quality)
	local circle = {}
	local tmp = 0
	for i = 1, quality do
		tmp = rad((i*360 / quality) + 90)
		circle[i] = {x = x + cos(tmp) * radius, y = y + sin(tmp) * radius}
	end
	return circle
end

function SITP_EconomyPaint ()
	if(SinglePlayer()) then return end
	
	local w = ScrW()
	local h = ScrH()
	
	local creds = LocalPlayer():GetNetworkedInt("sitp_creds")
	
	local text = "Credits: " .. tostring(creds)
	
	local x = w - 50 - string.len(text) * 11
	local y = 50
	
	surface.SetDrawColor( 0, 0, 0, 120)
	surface.DrawRect( x - 20 , y - 10 , string.len(text) * 11 + 20, 45 )
	
	surface.SetTextColor( 200, 200, 200, 255 )
	surface.SetTextPos( x, y ) 
	surface.SetFont( "Trebuchet24" )
	surface.DrawText( text )
end

function SITP_TeamPaint ()
	local everyone = player.GetAll()
	for k, v in pairs(everyone) do
		if(v:IsValid() and v != LocalPlayer()) then
			local frq = v:GetNetworkedString("sitp_team")
			local sfrq = LocalPlayer():GetNetworkedInt("sitp_team")
			local iplt = v:GetNetworkedString("sitp_spacetype")
			local siplt = LocalPlayer():GetNetworkedInt("sitp_spacetype")
			
			if((iplt == frq) or (siplt == frq) or (iplt == "all") or (siplt == "all")) then
				-- do nothing?
			else
				local Trg = (v:GetShootPos() + Vector(0, 0, 80))
				local Disp = LocalPlayer():GetShootPos() - Trg
				local len = math.sqrt((Disp.x)^2 + (Disp.y)^2 + (Disp.z)^2)
				local Scr = Trg:ToScreen()
		
				local scale = math.max(0.05 - 0.05 * len / 16000, 0.01) * 0.4
				local scrw = surface.ScreenWidth()
				local scrh = surface.ScreenHeight()
				local xPos = Scr.x
				local yPos = Scr.y
				--if(scale > 0) and isInsideZone(xPos, yPos, 0, 0, scrw, scrh) then
				if(scale > 0) then
					local r, g, b
					if(sfrq == "merc") then
						r = 255
						g = 255
						b = 0
					elseif (frq == sfrq) then
						r = 155
						g = 155
						b = 255
					elseif (frq != sfrq) then
						r = 200
						g = 50
						b = 50
					end
					surface.SetDrawColor(r, g, b, 255)
					--surface.DrawRect(xPos, yPos, scrw * scale, scrw * scale)
					local poly = GeneratePoly(xPos, yPos, scrw * scale, 3)
					surface.DrawPoly( poly ) 
				end
			end
		end
	end
end

hook.Add( "HUDPaint", "Economy Counter", SITP_EconomyPaint )
hook.Add( "HUDPaint", "Team Identifiers", SITP_TeamPaint )